using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.FaceBook;
using MicroMsg.Plugin.FaceBook.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace MicroMsg.UI.Page
{
	public class FaceBookFriends : BasePage
	{
		private const string TAG = "FaceBookFriends";
		private string currentToken = "";
		private ObservableCollection<FBContact> currentlist;
		private bool isNeedGoBack;
		private EventWatcher friendsSuccessWatcher;
		private EventWatcher friendsFailWatcher;
		private ApplicationBar mBar;
		private WebBrowser wbGetToken;
		private string tokenCode = "";
		private EventWatcher UpdateSvrTokenWatcher;
		private EventWatcher mWatchContactListChange;
		internal Grid LayoutRoot;
		internal DynamicTextBlock PageTitle;
		internal Grid ContentPanel;
		internal ListBox FriendList;
		internal TextBlock NoFriendsInfo;
		private bool _contentLoaded;
		public static void ShowFaceBookFriendsPage(BasePage originPage, string strToken, bool isRemoveMe = true)
		{
			originPage.GoToPage("/Source/Plugin/Facebook/UI/FaceBookFriends.xaml", new object[]
			{
				strToken
			}, isRemoveMe);
		}
		public FaceBookFriends()
		{
			this.InitializeComponent();
			base.set_ApplicationBar(this.GetApplicationBar());
		}
		protected override void OnPageBack()
		{
			if (this.isNeedGoBack)
			{
				base.get_Dispatcher().BeginInvoke(delegate
				{
					base.GoBack();
				});
			}
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 == null || Enumerable.Count<object>(object_0) <= 0)
			{
				return;
			}
			this.currentToken = (object_0[0] as string);
			this.InitFriends();
			this.InitContactChange();
			if (FaceBookMgr.IsTokenExpires())
			{
				FaceBookLoginPage.ShowFaceBookLoginPage(this, new FaceBookLoginCallBack(this.FaceBookLoginCB), false);
				return;
			}
			List<FBContact> list = new List<FBContact>();
			list = StorageMgr.fbContact.getList();
			this.RemoveMySelf(list);
			list.Sort(new Comparison<FBContact>(FaceBookFriends.SortFriends));
			ObservableCollection<FBContact> itemsSource = new ObservableCollection<FBContact>(list);
			this.FriendList.set_ItemsSource(itemsSource);
			this.currentlist = itemsSource;
			if (FBServiceCenter.fbContactMgr.getFBContact(false))
			{
				this.showProgressCover("loading...");
				return;
			}
			this.RefreshEmptyHint();
		}
		public void FaceBookLoginCB(bool isSuccess, string strToken)
		{
			if (!isSuccess)
			{
				this.isNeedGoBack = true;
				return;
			}
			if (string.IsNullOrEmpty(strToken))
			{
				return;
			}
			PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
			if (!(phoneApplicationFrame.get_Content() is BasePage))
			{
				return;
			}
			Account curAccount = AccountMgr.getCurAccount();
			if (string.IsNullOrEmpty(curAccount.FBToken))
			{
				return;
			}
			curAccount.FBToken = strToken;
			AccountMgr.updateAccount();
			if (FBServiceCenter.fbContactMgr.getFBContact(false))
			{
				this.showProgressCover("loading...");
				return;
			}
			List<FBContact> list = new List<FBContact>();
			list = StorageMgr.fbContact.getList();
			this.RemoveMySelf(list);
			list.Sort(new Comparison<FBContact>(FaceBookFriends.SortFriends));
			ObservableCollection<FBContact> itemsSource = new ObservableCollection<FBContact>(list);
			this.FriendList.set_ItemsSource(itemsSource);
			this.currentlist = itemsSource;
			this.RefreshEmptyHint();
		}
		private void RefreshEmptyHint()
		{
			ObservableCollection<FBContact> observableCollection = this.FriendList.get_ItemsSource() as ObservableCollection<FBContact>;
			if (observableCollection != null && observableCollection.get_Count() > 0)
			{
				this.NoFriendsInfo.set_Visibility(1);
				return;
			}
			this.NoFriendsInfo.set_Visibility(0);
		}
		protected override void OnPageOut()
		{
			this.UnitFriends();
			this.UnitContactChange();
		}
		private void RemoveMySelf(List<FBContact> list)
		{
			if (list == null || list.get_Count() <= 0)
			{
				return;
			}
			Account curAccount = AccountMgr.getCurAccount();
			if (curAccount == null)
			{
				return;
			}
			for (int i = 0; i < list.get_Count(); i++)
			{
				FBContact fBContact = list.get_Item(i);
				if (fBContact.strUsrName == curAccount.strUsrName)
				{
					list.RemoveAt(i);
					return;
				}
			}
		}
		private void InitFriends()
		{
			this.friendsSuccessWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onGetFriendsSuccess));
			this.friendsFailWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onGetFriendsFail));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, this.friendsSuccessWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_FAIL, this.friendsFailWatcher);
		}
		private void UnitFriends()
		{
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, this.friendsSuccessWatcher);
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_FAIL, this.friendsFailWatcher);
		}
		private void onGetFriendsSuccess(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<FBContact> list = new List<FBContact>();
			list = StorageMgr.fbContact.getList();
			if (this.currentlist != null && this.currentlist.get_Count() > 0)
			{
				for (int i = 0; i < this.currentlist.get_Count(); i++)
				{
					bool flag = false;
					using (List<FBContact>.Enumerator enumerator = list.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							FBContact current = enumerator.get_Current();
							if (this.currentlist.get_Item(i).strUsrName == current.strUsrName)
							{
								flag = true;
								break;
							}
						}
					}
					if (!flag)
					{
						this.currentlist.RemoveAt(i);
						i--;
					}
				}
				using (List<FBContact>.Enumerator enumerator2 = list.GetEnumerator())
				{
					IL_162:
					while (enumerator2.MoveNext())
					{
						FBContact current2 = enumerator2.get_Current();
						for (int j = 0; j < this.currentlist.get_Count(); j++)
						{
							if (this.currentlist.get_Item(j).strUsrName == current2.strUsrName)
							{
								this.currentlist.RemoveAt(j);
								IL_107:
								int num = -1;
								for (int k = 0; k < this.currentlist.get_Count(); k++)
								{
									if (FaceBookFriends.SortFriends(current2, this.currentlist.get_Item(k)) < 0)
									{
										num = k;
										IL_141:
										if (num == -1)
										{
											num = this.currentlist.get_Count();
										}
										this.currentlist.Insert(num, current2);
										goto IL_162;
									}
								}
								goto IL_141;
							}
						}
						goto IL_107;
					}
					goto IL_1A5;
				}
			}
			list.Sort(new Comparison<FBContact>(FaceBookFriends.SortFriends));
			ObservableCollection<FBContact> itemsSource = new ObservableCollection<FBContact>(list);
			this.FriendList.set_ItemsSource(itemsSource);
			IL_1A5:
			this.hideProgressCover();
			this.RefreshEmptyHint();
		}
		private void onGetFriendsFail(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			Log.smethod_1("FaceBookFriends", "onGetFriendsFail");
			this.hideProgressCover();
		}
		private static int SortFriends(FBContact fbcontact_0, FBContact fbcontact_1)
		{
			if (fbcontact_0 != null && fbcontact_1 != null)
			{
				return FaceBookFriends.getWeight(fbcontact_0.nStatus) - FaceBookFriends.getWeight(fbcontact_1.nStatus);
			}
			return -1;
		}
		private static int getWeight(int nStatus)
		{
			if (nStatus == 1)
			{
				return 0;
			}
			if (nStatus == 2)
			{
				return 2;
			}
			return 1;
		}
		private void showProgressCover(string strCoverTitle)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strCoverTitle, null, false, false, null);
		}
		private void hideProgressCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		public IApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Opacity(0.7);
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("/Resource/Img/CommonUse/icon_search.png", 2));
			applicationBarIconButton.set_Text(strings.COM_Search);
			applicationBarIconButton.add_Click(new EventHandler(this.searchButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton);
			ApplicationBarIconButton applicationBarIconButton2 = new ApplicationBarIconButton(new Uri("/Resource/Img/Plugin/FaceBook/icon_AppBar_Refresh.png", 2));
			applicationBarIconButton2.set_Text(strings.Set_Feedback_Refresh);
			applicationBarIconButton2.add_Click(new EventHandler(this.refreshButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton2);
			return this.mBar;
		}
		private void refreshButton_Click(object sender, EventArgs e)
		{
			this.showProgressCover("loading...");
			this.updateFbToken();
		}
		private void searchButton_Click(object sender, EventArgs e)
		{
			List<SearchItem> list = new List<SearchItem>();
			int type = 7;
			if (this.currentlist != null)
			{
				using (IEnumerator<FBContact> enumerator = this.currentlist.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						FBContact current = enumerator.get_Current();
						list.Add(new SearchItem(current, FaceBookMgr.GetFaceBookShowName(current), type, null));
					}
				}
			}
			SearchAlgorithm searchAlgorithm = new SearchAlgorithm(list);
			ToolFunction.GotoPageForUserControl("/Source/UI/ListSearchHelper/ListSearchPage.xaml", new object[]
			{
				searchAlgorithm
			}, false);
		}
		private void updateFbToken()
		{
			if (this.wbGetToken == null)
			{
				this.wbGetToken = new WebBrowser();
			}
			this.wbGetToken.add_LoadCompleted(new LoadCompletedEventHandler(this.wbGetToken_LoadCompleted));
			Uri loginUri = FBUris.GetLoginUri();
			this.wbGetToken.Navigate(loginUri);
		}
		private void wbGetToken_LoadCompleted(object sender, NavigationEventArgs e)
		{
			string text = e.get_Uri().get_OriginalString().ToLower();
			if (text.StartsWith("http://www.facebook.com/connect/login_success.html?code="))
			{
				this.tokenCode = e.get_Uri().get_OriginalString().Substring(56);
				this.wbGetToken.Navigate(FBUris.GetTokenLoadUri(this.tokenCode));
				return;
			}
			if (text.StartsWith("https://graph.facebook.com/oauth/access_token"))
			{
				string strExpires = "";
				string accessToken = FaceBookLoginPage.getAccessToken(this.wbGetToken.SaveToString(), ref strExpires);
				if (string.IsNullOrEmpty(accessToken))
				{
					this.hideProgressCover();
					return;
				}
				FaceBookMgr.UpdateTokenTime(strExpires);
				this.InitWatcher();
				NetSceneFaceBookAuth netSceneFaceBookAuth = new NetSceneFaceBookAuth();
				netSceneFaceBookAuth.doScene(2, accessToken);
			}
		}
		private void InitWatcher()
		{
			if (this.UpdateSvrTokenWatcher != null)
			{
				return;
			}
			this.UpdateSvrTokenWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onUpdateSvrTokenEvent));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_FACEBOOK_EVENT, this.UpdateSvrTokenWatcher);
		}
		private void UnitWatcher()
		{
			if (this.UpdateSvrTokenWatcher == null)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_FACEBOOK_EVENT, this.UpdateSvrTokenWatcher);
			this.UpdateSvrTokenWatcher = null;
		}
		private void onUpdateSvrTokenEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			FBAuthRet fBAuthRet = evtArgs.mObject as FBAuthRet;
			if (fBAuthRet != null)
			{
				if (fBAuthRet.opType == 2)
				{
					this.UnitWatcher();
					FBServiceCenter.fbContactMgr.getFBContact(true);
					return;
				}
			}
		}
		private void InitContactChange()
		{
			if (this.mWatchContactListChange == null)
			{
				this.mWatchContactListChange = new EventWatcher(this, this, new EventHandlerDelegate(this.onContactChange));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchContactListChange);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_DEL, this.mWatchContactListChange);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_MODIFY, this.mWatchContactListChange);
			}
		}
		private void UnitContactChange()
		{
			if (this.mWatchContactListChange != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchContactListChange);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_DEL, this.mWatchContactListChange);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_MODIFY, this.mWatchContactListChange);
				this.mWatchContactListChange = null;
			}
		}
		private void onContactChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (EventConst.ON_STORAGE_CONTACT_ADD == evtArgs.mEventID)
			{
				Log.smethod_5("FaceBookFriends", "ON_STG_CONTACT_ADD");
				List<Contact> contactListFromEventArg = this.getContactListFromEventArg(evtArgs);
				this.UpdateList(contactListFromEventArg);
				return;
			}
			if (EventConst.ON_STORAGE_CONTACT_MODIFY == evtArgs.mEventID)
			{
				Log.smethod_5("FaceBookFriends", "ON_STG_CONTACT_MOD start");
				List<Contact> contactListFromEventArg2 = this.getContactListFromEventArg(evtArgs);
				this.UpdateList(contactListFromEventArg2);
				Log.smethod_5("FaceBookFriends", "ON_STG_CONTACT_MOD end");
				return;
			}
			if (EventConst.ON_STORAGE_CONTACT_DEL == evtArgs.mEventID)
			{
				Log.smethod_5("FaceBookFriends", "ON_STG_CONTACT_DEL");
				List<Contact> contactListFromEventArg3 = this.getContactListFromEventArg(evtArgs);
				this.DeleteList(contactListFromEventArg3);
			}
		}
		private List<Contact> getContactListFromEventArg(BaseEventArgs evtArgs)
		{
			return evtArgs.getListObject<Contact>();
		}
		public void UpdateList(List<Contact> list)
		{
			if (list != null && list.get_Count() > 0)
			{
				using (List<Contact>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Contact current = enumerator.get_Current();
						this.UpdateItem(current);
					}
				}
				return;
			}
		}
		public void DeleteList(List<Contact> list)
		{
			if (list != null && list.get_Count() > 0)
			{
				using (List<Contact>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Contact current = enumerator.get_Current();
						this.RemoveItem(current);
					}
				}
				return;
			}
		}
		public void UpdateItem(Contact contact_0)
		{
			if (this.currentlist == null)
			{
				return;
			}
			if (contact_0 == null)
			{
				return;
			}
			FBContact fBContact = null;
			int i = 0;
			while (i < this.currentlist.get_Count())
			{
				if (!(this.currentlist.get_Item(i).strUsrName == contact_0.strUsrName))
				{
					i++;
				}
				else
				{
					fBContact = this.currentlist.get_Item(i);
					this.currentlist.RemoveAt(i);
					IL_5E:
					if (fBContact == null)
					{
						return;
					}
					if (ContactMgr.isContact(contact_0))
					{
						fBContact.nStatus = 2;
					}
					else
					{
						fBContact.nStatus = 1;
					}
					StorageMgr.fbContact.update(fBContact);
					int num = -1;
					for (int j = 0; j < this.currentlist.get_Count(); j++)
					{
						if (FaceBookFriends.SortFriends(fBContact, this.currentlist.get_Item(j)) < 0)
						{
							num = j;
							IL_B7:
							if (num == -1)
							{
								num = this.currentlist.get_Count();
							}
							this.currentlist.Insert(num, fBContact);
							return;
						}
					}
					goto IL_B7;
				}
			}
			goto IL_5E;
		}
		public void RemoveItem(Contact contact_0)
		{
			if (this.currentlist == null)
			{
				return;
			}
			if (contact_0 == null)
			{
				return;
			}
			for (int i = 0; i < this.currentlist.get_Count(); i++)
			{
				if (this.currentlist.get_Item(i).strUsrName == contact_0.strUsrName)
				{
					FBContact item = this.currentlist.get_Item(i);
					this.currentlist.RemoveAt(i);
					StorageMgr.fbContact.method_4(item);
					return;
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Facebook/UI/FaceBookFriends.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.PageTitle = (DynamicTextBlock)base.FindName("PageTitle");
			this.ContentPanel = (Grid)base.FindName("ContentPanel");
			this.FriendList = (ListBox)base.FindName("FriendList");
			this.NoFriendsInfo = (TextBlock)base.FindName("NoFriendsInfo");
		}
	}
}
